package com.xmg.client.mgrsite.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xmg.shop.api.domain.Catalog;
import com.xmg.shop.api.service.ICatalogService;
import com.xmg.shop.api.util.AjaxResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class CatalogController {
    @Reference
    private ICatalogService catalogService;

    @RequestMapping("/catalog/get")
    @ResponseBody
    public List<Catalog> getCatalog(Long id) {
        List<Catalog> cs = catalogService.getChildenCatalogById(id);
        return cs;
    }

    @RequestMapping("/catalogPage")
    public String catalogPage(Model model){
        model.addAttribute("catalogParent",catalogService.selectAll());
        return "catalog";
    }

    @RequestMapping("/getParentNode")
    @ResponseBody
    public List<Catalog> getParentNode(){
        return catalogService.getParentNode();
    }

    @RequestMapping("/catalogMenu")
    @ResponseBody
    public List<Catalog> childNode(Long id){
        return catalogService.getChildNode(id);
    }

    @RequestMapping("/catalog_save")
    @ResponseBody
    public AjaxResult save(String code,String name,Long parentCatalogId){
        try {
            catalogService.save(code,name,parentCatalogId);
            return new AjaxResult(true,"");
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    @RequestMapping("/catalog_update")
    @ResponseBody
    public AjaxResult update(Catalog catalog){
        try {
            catalogService.update(catalog);
            return new AjaxResult(true,"成功");
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult("失败");
        }
    }

    @RequestMapping("/deleteChilds")
    @ResponseBody
    public AjaxResult deleteChilds(Long[] id){
        try {
            catalogService.deleteChilds(id);
            return new AjaxResult(true,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult("删除失败");
        }
    }
}